package com.xuetang9.fujian.socketdemo.udpdemo.onetoone;

import java.io.IOException;
import java.net.*;

/**
 * @author: fujian
 * @description:
 * @date: 2021/5/9 15:04
 * @version: V1.0
 * @package: com.xuetang9.fujian.socketdemo.udpdemo.onetoone
 **/
public class UDPClient {
    public static void main(String[] args) throws IOException {
        String message = "我要点菜";
        InetAddress serverAddress = InetAddress.getByName("localhost");
        DatagramPacket packet = new DatagramPacket(message.getBytes(),message.getBytes().length,
                                                    serverAddress,
                                                9527
                                                    );
        DatagramSocket clientSocket = new DatagramSocket();
        clientSocket.send(packet);


        byte[] packetBuffer = new byte[2048];
        packet = new DatagramPacket(packetBuffer,packetBuffer.length);
        clientSocket.receive(packet);
        message = new String(packetBuffer,0,packet.getLength());
        System.out.println("成功接收到来自客户端的消息：" + message);
    }
}
